<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="5" :sm="15">
            <a-form-item label="部门名称" >
              <a-input placeholder="请输入" v-model="queryParam.deptName"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="状态">
              <a-select placeholder="请选择" v-model="queryParam.status">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="0">正常</a-select-option>
                <a-select-option value="1">停用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="this.fetch">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button v-if="addEnable" type="primary" icon="plus-circle" shape="round" @click="$refs.modal.add()">新建</a-button>
    </div>
    <a-table
      ref="table"
      rowKey="id"
      :pagination="pagination"
      :loading="loading"
      :columns="columns"
      :dataSource="data">

      <span slot="status" slot-scope="text">
        {{ text | statusFilter }}
      </span>

      <span slot="action" slot-scope="text, record">
        <a v-if="editEnable" @click="handleEdit(record)"><a-icon type="edit"/>  编辑</a>
        <a-divider type="vertical" />
        <a v-if="addEnable" @click="handleAdd(record.deptId)"><a-icon type="plus"/>  新增</a>
        <a-divider v-if="removeEnable && record.parentId !=='0'" type="vertical" />
        <a v-if="removeEnable && record.parentId !=='0'" @click="delById(record.deptId)"><a-icon type="delete"/>  删除</a>
      </span>
    </a-table>

    <!--<dept-modal ref="modal" @ok="handleOk"/>-->
  </a-card>
</template>

<script>
import { Table as T } from 'ant-design-vue'
import { checkPermission } from '@/utils/permissions'
import { findDeptList } from '@/api/system'
import { treeData } from '@/utils/util'
export default {
  name: 'DeptList',
  components: {
    T
  },
  data () {
    return {
      mdl: {},
      // 表头
      columns: [
        {
          title: '部门名称',
          dataIndex: 'deptName'
        },
        {
          title: '排序',
          dataIndex: 'orderNum',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '操作',
          width: '300px',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 查询参数
      queryParam: {},
      data: [],
      pagination: false,
      loading: false,
      addEnable: checkPermission('system:dept:add'),
      editEnable: checkPermission('system:dept:edit'),
      removeEnable: checkPermission('system:dept:remove')
    }
  },
  filters: {
    statusFilter (status) {
      const statusMap = {
        false: '停用',
        true: '正常'
      }
      return statusMap[status]
    }
  },
  created () {
    this.fetch()
  },
  methods: {
    fetch () {
      this.loading = true
      findDeptList(Object.assign(this.queryParam)).then(res => {
        this.data = treeData(res.data, 'id')
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
